package sql.parser;

import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
@Getter
@Setter
public class SyntaxNode {
    private SyntaxNodeType nodeType;
    private Object token;
    private String alias;
    private List<SyntaxNode> children = Lists.newArrayList();

    private SyntaxNode selectNode;
    private SyntaxNode updateNode;
    private SyntaxNode fromNode;
    private SyntaxNode whereNode;
    private SyntaxNode orderByNode;
    private SyntaxNode limitNode;

    public SyntaxNode(SyntaxNodeType nodeType) {
        this.nodeType = nodeType;
    }

    public SyntaxNode(SyntaxNodeType nodeType, Object token) {
        this.nodeType = nodeType;
        this.token = token;
    }

    public SyntaxNode(SyntaxNodeType nodeType, Object token, String alias) {
        this.nodeType = nodeType;
        this.token = token;
        this.alias = alias;
    }

    public void addChild(SyntaxNode node) {
        children.add(node);
    }

    public String getTokenAsString() {
        if(this.token == null) {
            return null;
        } else if(this.token instanceof String) {
            return (String) this.token;
        } else {
            return String.valueOf(token);
        }
    }
}
